<template>
  <div class="app-container">
    <el-row>
      <!-- 搜索区域 -->
      <el-card style="margin:0 0 5px 5px" v-show="showSearch">
        <el-form ref="queryForm" :inline="true" :model="queryData">
          <el-form-item label="流程名称" prop="Q_processName_S_LK">
            <el-input v-model="queryData.Q_processName_S_LK" placeholder="流程名称" />
          </el-form-item>
          <el-form-item label="发起人" prop="Q_originatorName_S_LK">
            <el-input v-model="queryData.Q_originatorName_S_LK" placeholder="发起人" />
          </el-form-item>
          <el-form-item class="fr p-r-10">
            <el-button class="filter-item" type="primary" round icon="el-icon-search" @click="handleFilter">
              搜索
            </el-button>
            <el-button class="filter-item m-l-10" type="info" round icon="el-icon-refresh" @click="handleClear">
              清空
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- table区域 -->
      <el-card class="m-l-5">
        <el-row :gutter="10" class="m-b-5">
          <table-toolbar :showSearch.sync="showSearch" @refresh="getTableData" />
        </el-row>
        <el-table
          ref="copy_table"
          v-loading="tableLoading"
          row-key="copyId"
          :data="tableData"
          element-loading-text="加载中"
          border
          fit
          highlight-current-row
          show-overflow-tooltip
          height="100px"
          v-el-height-adaptive-table="{bottomOffset: 110}"
        >
          <el-table-column align="center" label="流程名称" prop="processName" width="300" show-overflow-tooltip />
          <el-table-column align="center" label="标题" prop="title" show-overflow-tooltip />
          <el-table-column align="center" label="发起人" prop="originatorName" width="150" show-overflow-tooltip />
          <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
          <el-table-column align="center" label="操作" width="80" fixed="right">
            <template slot-scope="scope">
              <el-link type="info" icon="el-icon-tickets" @click="handleDetail(scope.row)">详情</el-link>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="tableQuery.total>0" :total="tableQuery.total" :page.sync="tableQuery.page" :limit.sync="tableQuery.limit" @pagination="getTableData" />
      </el-card>
    </el-row>

    <task-detail-dialog v-if="openTaskDetailDialog" ref="taskDetailDialog" @confirm="handleFilter" />
  </div>
</template>

<script>
import { copyTable } from '@/api/tool/workflow'
import TaskDetailDialog from './detail'

export default {
  components: {
    TaskDetailDialog
  },
  data() {
    return {
      showSearch: true,
      queryData: {
        Q_processName_S_LK: undefined,
        Q_originatorName_S_LK: undefined
      },
      tableLoading: false,
      tableData: [],
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: undefined,
        order: undefined
      },
      openTaskDetailDialog: false
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getTableData()
    },
    handleFilter() {
      this.tableQuery.page = 1
      this.getTableData()
    },
    handleClear() {
      this.$refs.queryForm.resetFields()
      this.getTableData()
    },
    getTableData() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.tableLoading = true
      copyTable(query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
        this.$refs.copy_table.bodyWrapper.scrollTop = 0
      }).catch(e => {
        this.tableLoading = false
      })
    },
    sortChange(data) {
      const { prop, order } = data
      this.tableQuery.sort = prop
      if (order === 'ascending') {
        this.tableQuery.order = 'asc'
      } else {
        this.tableQuery.order = 'desc'
      }
      this.getTableData()
    },
    handleDetail(row) {
      this.openTaskDetailDialog = true
      this.$nextTick(() => {
        this.$refs.taskDetailDialog.init({
          definitionId: row.processId,
          procInsId: row.instanceId,
          deployId: row.deploymentId,
          taskId: row.taskId,
          finished: false
        })
      })
    }
  }
}
</script>
